//AccountBaseServiceImpl.java
package com.lqjava.tuxiaowang.tuxiaowang.serverapp.app.service.impl;


import cn.hutool.crypto.digest.MD5;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lqjava.tuxiaowang.tuxiaowang.beans.convertor.AccountBaseConvertor;
import com.lqjava.tuxiaowang.tuxiaowang.beans.entity.domain.AccountBase;
import com.lqjava.tuxiaowang.tuxiaowang.beans.entity.request.IDRequest;
import com.lqjava.tuxiaowang.tuxiaowang.beans.entity.request.AccountBaseOptionRequest;
import com.lqjava.tuxiaowang.tuxiaowang.beans.entity.request.AccountBaseQuesrRequest;
import com.lqjava.tuxiaowang.tuxiaowang.beans.entity.request.LoginRequest;
import com.lqjava.tuxiaowang.tuxiaowang.beans.entity.response.AccountBaseViewResponse;
import com.lqjava.tuxiaowang.tuxiaowang.beans.exceptions.ServiceException;
import com.lqjava.tuxiaowang.tuxiaowang.beans.mapper.AccountBaseMapper;
import com.lqjava.tuxiaowang.tuxiaowang.beans.mapper.common.ICRUDMapper;
import com.lqjava.tuxiaowang.tuxiaowang.serverapp.app.cache.Cacher;
import com.lqjava.tuxiaowang.tuxiaowang.serverapp.app.service.AccountBaseService;
import com.lqjava.tuxiaowang.tuxiaowang.serverapp.app.service.common.ABSCRUDServiceImpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class AccountBaseServiceImpl extends ABSCRUDServiceImpl<AccountBase> implements AccountBaseService {

    @Autowired
    private AccountBaseMapper mapper;

    @Autowired
    private AccountBaseConvertor convertor;

    @Autowired
    private Cacher cacher;

    @Override
    public ICRUDMapper<AccountBase> getBaseMapper() {
        return mapper;
    }

    @Override
    public AccountBaseViewResponse getView(IDRequest request) {
        if(request == null) ServiceException.throwBusiness("查询参数不能为空");
        if(request.getId() == null) ServiceException.throwBusiness("id不能为空");
        AccountBase entity = mapper.selectById(request.getId());
        if(entity == null) ServiceException.throwBusiness("实体不存在或者已经被删除");
        return convertor.convert2View(entity);
    }

    @Override
    public List<AccountBaseViewResponse> listView(AccountBaseQuesrRequest request) {
        List<AccountBase> entityList = mapper.selectList(request.getQueryWrapper());
        return convertor.convert2ViewList(entityList);
    }

    @Override
    public IPage<AccountBaseViewResponse> pageView(AccountBaseQuesrRequest request) {
        IPage<AccountBaseViewResponse> page = new Page<>();
        IPage<AccountBase> result = mapper.selectPage(request.getPage(), request.getQueryWrapper());
        page.setPages(result.getPages());
        page.setCurrent(result.getCurrent());
        page.setSize(result.getSize());
        page.setTotal(result.getTotal());
        page.setRecords(convertor.convert2ViewList(result.getRecords()));
        return page;
    }


    @Override
    public Long insert(AccountBaseOptionRequest request) {
        if(request.getAccount() == null) ServiceException.throwBusiness("账号不能为空");
        if(request.getPwd() == null) ServiceException.throwBusiness("密码不能为空");
        if(this.getByAccount(request.getAccount()) != null)  ServiceException.throwBusiness("账号已存在");

        AccountBase entity = convertor.convert(request);
        request.setPwd(new String(MD5.create().digest(String.format("%s_%s",entity.getAccount(),entity.getPwd()))));
        convertor.initDefValue(entity,true);
        return super.insert(entity);
    }
    @Override
    public Long update(AccountBaseOptionRequest request) {
        //账号不可修改
        request.setAccount(null);

        this.getView(new IDRequest(request.getId()));
        AccountBase entity = convertor.convert(request);
        convertor.initDefValue(entity,false);
        return super.update(entity);
    }
    @Override
    public Long save(AccountBaseOptionRequest request) {
        return request.getId() == null ? this.insert(request) : this.update(request);
    }
    @Override
    public Long del(IDRequest request) {
        this.getView(request);
        return super.del(request);
    }

    @Override
    public String login(LoginRequest request) {
        AccountBase base = getByAccount(request.getAccount());
        if(base == null) ServiceException.throwBusiness("账号不存在");
        if(!base.getPwd().equals(request.getPwd())) ServiceException.throwBusiness("账号或者密码错误");
        return cacher.LOGIN.login(base.getId(),base.getAccount());
    }


    private AccountBase getByAccount(String account){
        if(account == null) ServiceException.throwBusiness("账号不能为空");
        return mapper.selectOne(new LambdaQueryWrapper<AccountBase>().eq(AccountBase::getAccount, account));
    }
}

